import random
import string
import time
from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys

__author__ = 'e_kulgavaya'


class CommonTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(10)
        self.base_url = "http://localhost/"
        self.open_home_page()
        self.login("admin", "admin")

    def tearDown(self):
        self.driver.quit()

    def clear_and_send_keys(self, field_name, value):
        field = self.driver.find_element_by_name(field_name)
        field.clear()
        field.send_keys(value)

    def open_home_page(self):
        self.driver.get(self.base_url + "/php4dvd/")

    def login(self, login, password):
        driver = self.driver
        self.clear_and_send_keys("username", login)
        self.clear_and_send_keys("password", password)
        driver.find_element_by_name("submit").click()

    def click_button(self, button_title):
        css_selector = "img[title='{0}']".format(button_title)
        self.driver.find_element_by_css_selector(css_selector).click()

    def fill_required_fields(self, title, year):
        self.clear_and_send_keys("name", title)
        self.clear_and_send_keys("year", year)

    def add_movie(self, title, year):
        self.open_home_page()
        self.click_button("Add movie")
        self.fill_required_fields(title, year)
        self.click_button("Save")

    def open_movie_page(self, title):
        css_selector = "div#results div[id^='movie_'] div[title='{0}']".format(title)
        movie = self.driver.find_element_by_css_selector(css_selector)
        movie.click()

    def get_movies_titles(self):
        title_labels = self.driver.find_elements_by_css_selector("div#results div[id^='movie_'] div.title")
        titles = [label.text for label in title_labels]
        return titles

    def delete_movie(self, title):
        self.open_movie_page(title)
        self.click_button("Remove")
        self.driver.switch_to_alert().accept()
        self.driver.switch_to_default_content()

    def is_movie_displayed_on_homepage(self, title):
        self.open_home_page()
        all_titles = self.get_movies_titles()
        return title in all_titles

    def search(self, keyword):
        self.open_home_page()
        search_field = self.driver.find_element_by_id('q')
        search_field.clear()
        search_field.send_keys(keyword)
        search_field.send_keys(Keys.RETURN)
        time.sleep(1)  # TODO - update with waiting for some actions to complete

    def verify_error_is_displayed_for_field(self, field_name):
        css_selector = "label.error[for='{0}']".format(field_name)
        error_label = self.driver.find_element_by_css_selector(css_selector)
        assert error_label.is_displayed()

    def verify_saved_movie_info(self, title, year):
        expected_name = "{0} ({1})".format(title, year)
        actual_name = self.driver.find_element_by_css_selector("div#movie>div.maininfo_full>h2").text
        assert expected_name == actual_name


def generate_movie_title(size=7, chars=string.ascii_letters + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


def generate_movie_year():
    return random.choice(range(1895, 2014))
